<?php

namespace App\Http\Controllers;

use App\Models\Device;
use App\Models\DeviceGroupAdd;
use DB;
use Validator;
use Carbon\Carbon;
use App\Models\DeviceGroupSentence;
use App\Models\DeviceGroup;
use Illuminate\Http\Request;

class deviceGroupSentenceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'required|in:1,2,3'
        ]);
        if ($validator->fails()) {
            return $this->failure(-1, $validator->errors()->first());
        }
        $perPage = $request->perpage ? $request->perpage : 10;
        $query = DeviceGroupSentence::where('type',$request->type)
            ->orderBy('priority', 'desc');
        if($request->device_group_id > 0) {
            $query->where('device_group_id', $request->device_group_id);
        }
        $device_group_sentences = $query->paginate($perPage)->toArray();
        return $this->success(0, '', $device_group_sentences);
    }

    public function GroupWechat(Request $request)
    {
        if($request->id > 0){
            $add = DeviceGroupAdd::find($request->id);
            $add->wechat1 = $request->wechat1;
            $add->wechat2 = $request->wechat2;
            $add->wechat3 = $request->wechat3;
            $add->wechat4 = $request->wechat4;
            $add->save();
            return $this->success(0, '', $add);
        }else{
            $data = DeviceGroupAdd::where('device_group_id', $request->device_group_id)->get();
            return $this->success(0, '', $data);
        }
    }

    public function setDeviceGroup(Request $request)
    {
        $sentences = DeviceGroupSentence::find($request->id);
        if(!$sentences) {
            return $this->failure(-1, '话术不存在');
        }
        $validator = Validator::make($request->all(), [
            'device_group_id' => 'required|exists:device_groups,id',
        ]);
        if($validator->fails()) {
            return $this->failure(-1, $validator->errors()->first());
        }
        $sentences->device_group_id = $request->device_group_id;
        $sentences->save();
        return $this->success(0, '', $sentences);
    }

    public function getOne(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'sometimes|required|in:1,2,3',//为了不影响旧接口Tim，写为sometimes，即0不需要
            'device_id' => 'required|exists:devices,id'
        ]);
        if($validator->fails()) {
            return $this->failure(-1, $validator->errors()->first());
        }
        $type = $request->type ? $request->type : 1;
        $device_group_id = Device::find($request->device_id)->device_group_id;
        $deviceGroupAdd = DeviceGroupAdd::where('device_group_id', $device_group_id)->get();

        $device_group_sentences = DeviceGroupSentence::where('type', $type)
            ->where('device_group_id', $device_group_id)
            ->where('priority', 100)
            ->orderBy(DB::raw('rand()'))
            ->first();
        if(!$device_group_sentences)
            $device_group_sentences = DeviceGroupSentence::where('type', $type)
                ->where('device_group_id', $device_group_id)
                ->orderBy(DB::raw('rand()'))
                ->first();
        if(!$device_group_sentences)
            return $this->success(-1, '无数据', null);

        unset(
            $device_group_sentences['created_at'],
            $device_group_sentences['updated_at'],
            $deviceGroupAdd[0]['created_at'],
            $deviceGroupAdd[0]['updated_at'],
            $deviceGroupAdd[0]['device_group_id'],
            $deviceGroupAdd[0]['id'],
            $deviceGroupAdd[0]['cover_qq']
        );
//        $this->deviceDetailedList($type=1,$detail_type=5,null);
        $device_group_sentences['wechat'] = $deviceGroupAdd;
        return $this->success(0, '', $device_group_sentences);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'required|in:1,2,3',
            'device_group_id' => 'required|exists:device_groups,id',
            'priority' => 'required|numeric|max:100,min:0'
        ]);
        if ($validator->fails()) {
            return $this->failure(-1, $validator->errors()->first());
        }
        $cred = $request->only('type','wechat_name', 'content1', 'content2', 'device_group_id', 'priority');
        $device_group_sentence = DeviceGroupSentence::create($cred);
        return $this->success(0, '', $device_group_sentence);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'device_group_id' => 'sometimes|required|exists:device_groups,id',
            'priority' => 'sometimes|required|numeric|max:100,min:0'
        ]);
        if ($validator->fails()) {
            return $this->failure(-1, $validator->errors()->first());
        }
        $device_group_sentence = DeviceGroupSentence::findOrFail($id);
        $cred = $request->only('wechat_name', 'content1', 'content2', 'device_group_id', 'priority');
        $device_group_sentence->update($cred);
        return $this->success(0, '', $device_group_sentence);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DeviceGroupSentence::destroy($id);
        return $this->success(0, '删除成功');
    }


    public function deleteAll(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'required|in:1,2,3'
        ]);
        if ($validator->fails()) {
            return $this->failure(-1, $validator->errors()->first());
        }
        if($request->device_group_id>0){
            DeviceGroupSentence::where('type', $request->type)
                ->where('device_group_id', $request->device_group_id)
                ->delete();
        }else{
            DeviceGroupSentence::where('type', $request->type)
                ->where('priority','>=',1)->delete();
        }
        return $this->success(0, '');
    }
}
